home *** CD-ROM | disk | FTP | other *** search
/ MacWorld Secrets (4th Edition) / Mac Secrets CD 4th Ed.toast / Apple Advanced Technologies / Apple Speech Technologies 1.5 / PlainTalk Developer Info / Speech Recognition Manager SDK / SR Sample Code / IM SR Example / MyBuildLanguageModel.c < prev    next >
C/C++ Source or Header  |  1996-04-10  |  4KB  |  124 lines

  1. #include <SpeechRecognition.h>
  2. #include <string.h>
  3.  
  4. SRLanguageModel MyBuildLanguageModel (SRRecognitionSystem mySystem);
  5.  
  6. /* creates language model for the following BNF:
  7. <TopLM>    = <call> <person> | schedule meeting with <person> | view today's schedule;
  8. <call>    = call | phone | dial;
  9. <person>= Arlo | Matt | Brent | my wife;
  10. */
  11.  
  12. const long        kTopLMRefCon = 'top ';
  13. const long        kCallPersonRefCon = 'call';
  14.  
  15. const char        kCallLMName[] = "<call>";
  16. const char *    kCallSynonyms[] = {"call", "phone", "dial", NULL};
  17.  
  18. const char        kPersonLMName[] = "<person>";
  19. const char *    kPersonNames[] = {"Arlo", "Matt", "Brent", "my wife", NULL};
  20.  
  21. const char        kTomLMName[] = "<TopLM>";
  22. const char        kScheduleMeetingWith[] = "schedule meeting with";
  23. const char        kViewTodaysSchedule[] = "view today's schedule";
  24.  
  25. SRLanguageModel MyBuildLanguageModel (SRRecognitionSystem mySystem)
  26. {
  27.     OSErr            myErr = noErr;
  28.     SRLanguageModel    myCallLM = 0, myPersonLM = 0, myTopLM = 0;
  29.     SRPath            myPath;
  30.     char **            myStringArray;
  31.     char *            myCurrString;
  32.     
  33.     // create an embedded language model named "<call>"
  34.     myErr = SRNewLanguageModel (mySystem, &myCallLM, kCallLMName, strlen(kCallLMName));
  35.     if (!myErr) {
  36.         SRPhrase    myPhrase;
  37.         
  38.         myStringArray = (char **) kCallSynonyms;
  39.         while ((myCurrString = *myStringArray) != NULL) {
  40.             /* note that we call SRNewPhrase instead of SRNewWord     */
  41.             /* so that we don't have to know if any of the synonyms */
  42.             /* is more than one word */
  43.             myErr = SRNewPhrase (mySystem, &myPhrase, myCurrString, strlen(myCurrString));
  44.             if (!myErr) {
  45.                 if (!myErr) myErr = SRAddLanguageObject (myCallLM, myPhrase);
  46.                 
  47.                 SRReleaseObject (myPhrase); /* balances SRNewPhrase */
  48.             }
  49.             
  50.             myStringArray++;
  51.         }
  52.     }
  53.     
  54.     /* create an embedded language model named "<person>"         */
  55.     /* Note that this code uses SRAddText, a useful shortcut    */
  56.     /* which calls SRNewPhrase internally                         */
  57.     if (!myErr) 
  58.         myErr = SRNewLanguageModel (mySystem, &myPersonLM, kPersonLMName, 
  59.                                                             strlen(kPersonLMName));
  60.     if (!myErr) {
  61.         long    myRefCon = 0;
  62.         
  63.         myStringArray = (char **) kPersonNames;
  64.         while ((myCurrString = *myStringArray) != NULL) {
  65.             myErr = SRAddText (myPersonLM, myCurrString, strlen(myCurrString), 
  66.                                                                     myRefCon++);
  67.             /* Note, we set the refcon in the SRAddText call, so we */
  68.             /* can use it later when processing the search result    */
  69.             
  70.             myStringArray++;
  71.         }
  72.     }
  73.  
  74.     /* create a top-level language model named "<TopLM>"     */
  75.     if (!myErr) 
  76.         myErr = SRNewLanguageModel (mySystem, &myTopLM, kTomLMName, 
  77.                                                             strlen(kTomLMName));
  78.     
  79.     /* we set the refcon of the top-level language model,     */
  80.     /* so that we can identify it in the search result         */
  81.     if (!myErr) myErr = SRSetProperty (myTopLM, kSRRefCon, &kTopLMRefCon, 
  82.                                                             sizeof (kTopLMRefCon));
  83.  
  84.     /* create a path for "<call> <person>" and add to "<TopLM>" */
  85.     if (!myErr) myErr = SRNewPath (mySystem, &myPath);
  86.     if (!myErr) {
  87.         if (!myErr) myErr = SRAddLanguageObject (myPath, myCallLM);
  88.         if (!myErr) myErr = SRAddLanguageObject (myPath, myPersonLM);
  89.         
  90.         if (!myErr) myErr = SRAddLanguageObject (myTopLM, myPath);
  91.         
  92.         /* we set the refcon of the path, so that we can identify    */
  93.         /* it in the search result                                     */
  94.         if (!myErr) myErr = SRSetProperty (myPath, kSRRefCon, &kCallPersonRefCon, 
  95.                                                         sizeof(kCallPersonRefCon));
  96.         
  97.         SRReleaseObject (myPath);  /* balances SRNewPath */
  98.     }
  99.  
  100.     /* create a path for "schedule meeting with <person>" and add to "<TopLM>" */
  101.     if (!myErr) myErr = SRNewPath (mySystem, &myPath);
  102.     if (!myErr) {
  103.         if (!myErr) myErr = SRAddText (myPath, kScheduleMeetingWith, 
  104.                                                     strlen(kScheduleMeetingWith), 0);
  105.         if (!myErr) myErr = SRAddLanguageObject (myPath, myPersonLM);
  106.         
  107.         if (!myErr) myErr = SRAddLanguageObject (myTopLM, myPath);
  108.         
  109.         SRReleaseObject (myPath);  /* balances SRNewPath */
  110.     }
  111.  
  112.     /* add "view today's schedule" to "<TopLM>" */
  113.     if (!myErr) myErr = SRAddText (myTopLM, kViewTodaysSchedule, 
  114.                                                     strlen(kViewTodaysSchedule), 0);
  115.     
  116.     if (myCallLM) SRReleaseObject (myCallLM);        /* balances SRNewLanguageModel */
  117.     if (myPersonLM) SRReleaseObject (myPersonLM);    /* balances SRNewLanguageModel */
  118.     
  119.     return myTopLM;
  120. }
  121.  
  122.  
  123.  
  124.